<script setup>
import {onUpdated, ref} from 'vue';
import {showToast} from 'vant';
import {createRouter, useRouter} from "vue-router";
import {getAddressList} from "@/api/address.js";
import {getUserLocal} from "@/utils/auth.js";

const router = useRouter();
const chosenAddressId = ref('1');
const list = ref([
  // {
  //   id: '1',
  //   name: '张三',
  //   tel: '13000000000',
  //   address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
  //   isDefault: true,
  // },
  // {
  //   id: '2',
  //   name: '李四',
  //   tel: '1310000000',
  //   address: '浙江省杭州市拱墅区莫干山路 50 号',
  // },
])

// const disabledList = [
//   {
//     id: '3',
//     name: '王五',
//     tel: '1320000000',
//     address: '浙江省杭州市滨江区江南大道 15 号',
//   },
// ];
const list_main = ref([])
const params = ref({uid: getUserLocal().uid})
getAddressList(params.value).then(result => {
  list.value = result.data.data
  console.log(list.value)
}).then(() => {
  for (let i = 0; i < list.value.length; i++) {
    list_main.value[i] = {
      id: list.value[i].addressId,
      name: list.value[i].receiver,
      tel: list.value[i].cellPhone,
      address: list.value[i].province + list.value[i].city + list.value[i].address,
      isDefault: list.value[i].isDefault === 1
    }
    console.log(list_main.value[i])
  }
})

// async function a() {
//   const params = ref({uid: getUserLocal().uid})
//   getAddressList(params.value).then(result => {
//     list.value = result.data.data
//     console.log(list.value)
//   })
//   for (let i = 0; i <= list.value.length; i++) {
//     await console.log(list.value[i])
//   }
// }
//
// await a()
const onAdd = () => {
  let addressInfo = {
    name: '',
    tel: '',
    province: '',
    city: '',
    county: '',
    addressDetail: '',
    isDefault: 0,
  };
  router.push({
    name: 'updateAddress',
    params: {
      addressInfo: JSON.stringify(addressInfo),
      addressId: -1,
    },
  });
};

function onEdit(item) {
if (item.address.indexOf('省') !== -1) {
  var provence = item.address.split('省')[0] + '省'
  var tmp = item.address.split('省')[1]
}else {
  var provence = item.address.split('市')[0] + '市'
  var tmp = item.address.substr(item.address.indexOf('市')+1)
}
  let city = tmp.split('市')[0] + '市'
  console.log(tmp)
  tmp = tmp.substr(tmp.indexOf('市')+1)
  let county = tmp.split('区')[0] + '区'
  // console.log(tmp)
  let addressInfo = {
    name: item.name,
    tel: item.tel,
    province: provence,
    city: city,
    county: county,
    addressDetail: tmp.split('区')[1],
    isDefault: item.isDefault,
  };
  console.log(addressInfo)
  console.log(item)
  router.push({
    name: 'updateAddress',
    params: {
      addressInfo: JSON.stringify(addressInfo),
      addressId: item.id,
    },
  });
}

onUpdated(() => {
  getAddressList(params.value).then(result => {
    list.value = result.data.data
    console.log(list.value)
  }).then(() => {
    for (let i = 0; i < list.value.length; i++) {
      list_main.value[i] = {
        id: list.value[i].addressId,
        name: list.value[i].receiver,
        tel: list.value[i].cellPhone,
        address: list.value[i].province + list.value[i].city + list.value[i].address,
        isDefault: list.value[i].isDefault === 1
      }
      console.log(list_main.value[i])
    }
  })
})

</script>

<template>
  <div>
    <van-nav-bar
        title="地址列表"
        left-text="返回"
        left-arrow
        @click-left="$router.push('/user')">
    </van-nav-bar>
    <van-address-list
        v-model="chosenAddressId"
        :list="list_main"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
    />
  </div>
</template>

<style scoped>

</style>